<%@page import="org.apache.jsp.ForumHtml.notifier_jsp"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">


var t = setInterval(function() {
		
	$.ajax({
		type : "GET",
		url : "notifier.jsp",
		async:false,
		success : parseXml,
		error:function(xhr,err){
			
			document.write(xhr.responseText);
			
			clearInterval(t);
		}
	});
}, 5000);



function parseXml(xml)
{
	
	 var msg = "";
	 var reason ="";
	 targetFrame = window.parent.frames[0].document;
  //find every Tutorial and print the author
  $(xml).find("MSG").each(function()
  {
	  msg = "";
	  switch (parseInt($(this).find("REASON").text())){
	  case <%=notifier_jsp.CREATORS_TOPIC_UPDATED%>: 
	  			reason = "one of your topics has updated";
	  			break;
	  case <%=notifier_jsp.FRIEND_ADDED_POST%>: 
		  		reason = "Your friend added new post";
	  			break;
	  case <%=notifier_jsp.FRIEND_UPDATED_POST%>: 
	  		reason = "Your friend updated a post";
			break;
	
	  case <%=notifier_jsp.FRIEND_DELETED_POST%>: 
	  		reason = "Your friend deleted a post";
			break;	
	
	  case <%=notifier_jsp.NEW_POST_IN_CURRENT_TOPIC%>: 
	  		reason = "New post has added to the topic that you are looking at";
	  		//alert("inside");
	  		if ($(this).find("USER").text()=="<%=session.getAttribute("userName")%>")
	  			return true;
	  		
	  		if ($("table",targetFrame).length == 0)
	  			return true;
	  		
	  		var ebutton ="";
	  		var dbutton = "";
			var user = "<%=(String)session.getAttribute("userName")%>";
	  		var permission = "<%=(String)session.getAttribute("permission")%>";
	  		var pid = $(this).find("POSTID").text();
	  		
	  		if ((permission=="ADMIN")||(user == $(this).find("USER").text()))
	  			{
	  				ebutton = "<td id=edit"+$(this).find("POSTID").text()+"><input type=button onClick="+"procLink("+"'"+pid+"') value='edit post'>";
	  				dbutton = "<br/><input type=button onClick="+"removePost("+"'"+pid+"') value='delete'></td>";
	  			}
	  			
	  		
	  		var row = "<tr id="+$(this).find("POSTID").text()+">"+
	  							"<td id=title"+$(this).find("POSTID").text()+">"+$(this).find("POST").text()+"</td>"+
	  							"<td id=content"+$(this).find("POSTID").text()+"><textarea readonly='readonly'>"+$(this).find("POSTCONTENT").text()+"</textarea></td>"+
	  							"<td id=creator"+$(this).find("POSTID").text()+">"+$(this).find("CREATOR").text()+"</td>"+
	  							"<td id=creation"+$(this).find("POSTID").text()+">"+$(this).find("CREATION").text()+"</td>"+
	  							"<td id=editing"+$(this).find("POSTID").text()+">"+$(this).find("EDITING").text()+"</td>"+
	  							ebutton+dbutton+
	  					"</tr>";
			
	  		
	  		
	  		$("table",targetFrame).append(row);
	        return true;
		//	break;
	  case <%=notifier_jsp.POST_UPDATED_IN_CURRENT_TOPIC%>: 
	  		reason = "post has been updated in the topic that you are looking at";
			
	 		 if ($(this).find("USER").text()=="<%=session.getAttribute("userName")%>")
			return true;
	 		 
	 		if ($("#"+$(this).find("POSTID").text(),targetFrame).length == 0)
	  			return true;
	 		
	 		
	 		var ebutton ="";
			var user = "<%=(String)session.getAttribute("userName")%>";
	  		var permission = "<%=(String)session.getAttribute("permission")%>";
	  		var pid = $(this).find("POSTID").text();
	  
	  		
	  		if ((permission=="ADMIN")||(user == $(this).find("CREATOR").text()))
	  			{
	  				ebutton = "<input type=button onClick="+"procLink("+"'"+pid+"') value='edit post'>"+
	  				"<br/><input type=button onClick="+"removePost("+"'"+pid+"') value='delete'>";
	  			}
	 		
	 		
	 		
	  
	  		$("#title"+$(this).find("POSTID").text(),targetFrame).html($(this).find("POST").text()); 
	  		$("#content"+$(this).find("POSTID").text(),targetFrame).html("<textarea readonly='readonly'>"+$(this).find("POSTCONTENT").text()+"</textarea>");
	  		$("#creator"+$(this).find("POSTID").text(),targetFrame).html($(this).find("CREATOR").text());
	  		$("#creation"+$(this).find("POSTID").text(),targetFrame).html($(this).find("CREATION").text());
	  		$("#editing"+$(this).find("POSTID").text(),targetFrame).html($(this).find("EDITING").text());
	  		
	  		
	  		if ($("#edit"+$(this).find("POSTID").text(),targetFrame).length != 0)
	  			{
	  				
	  				
	  				$("#edit"+$(this).find("POSTID").text(),targetFrame).html(ebutton);
	  			}
	  			
	  		return true;
	  		//break;
	  		
	  case <%= notifier_jsp.POST_DELETED_IN_CURRENT_TOPIC%>: 
	  		reason = "post has deleted  in the topic that you are looking at";
			$("#"+$(this).find("POSTID").text(),targetFrame).remove()
	  
	  		return true;
	  		//break;
	  		
	  
	  default : alert("big problem");
	  }
	  
	  
		var currentDate = new Date();
		  var day = currentDate.getDate();
		  var month = currentDate.getMonth();
		  var year = currentDate.getFullYear();
		  var hours = currentDate.getHours();
	  		var minutes = currentDate.getMinutes();

	  		if (minutes < 10)
	  		minutes = "0" + minutes;
			var time = day + "/" + month + "/" + year + " "+ hours + ":" + minutes;
			//alert(time);
	  
	  
	  
	  msg+= time+" - "+reason+".\n";
	 

	 msg+= "user : "+$(this).find("USER").text()+".\n";
	 msg+= "in forum : "+$(this).find("FORUM").text()+".\n";
	 msg+= "In topic : "+$(this).find("TOPIC").text()+".\n";
	 msg+= "Post title = "+$(this).find("POST").text()+".\n\n";
	 $("<option value=posts.jsp?topicId="+$(this).find("TOPICID").text()+"&topicName="+$(this).find("TOPIC").text()+"&forumId="+$(this).find("FORUMID").text()+">"+msg+"</option>").appendTo("#ComboBox");
	 $("#ComboBox option:last").attr('selected',true);
	 $("#goToPost").attr('disabled', false);
	 
  });
 

	  

}

$(document).ready(function() {
	$("#goToPost").attr('disabled', true);
	
	$("#ComboBox").change(function() {
		
		if (!$("#ComboBox option:selected").length)
			$("#goToPost").attr('disabled', true);    
		else
			$("#goToPost").attr('disabled', false);
		});
	
	$("#goToPost").click(function() {
		  //alert( $("#ComboBox").val());
		  window.open ($("#ComboBox").val(), 'leftside');

		});
	

  
	
	});
	
	


</script>

<h1> USER: <%=session.getAttribute("userName") %> is logged in.</h1>
<h2>pemission: <%=(String)session.getAttribute("permission")%></h2>
<p>notification box</p>
<select id="ComboBox" size=4>
        
</select>
<button id=goToPost type="button">go</button>

</body>
</html>